package com.mxnet.chongjin.aviationtown;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

import com.mxnet.chongjin.aviationtown.utils.X5WebView;

import java.util.Timer;
import java.util.TimerTask;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private X5WebView mWebView;
    private Timer mTimer;
    private ImageView mImageView;
    private final String url = "http://jd.mxnet.cn/app/index.html#/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;

        mWebView = (X5WebView) findViewById(R.id.webview_main);
        mWebView.loadUrl(url);

        // 首页加载flash图片，延时2秒隐藏，此时webview已加载完所需资源
        mImageView = findViewById(R.id.imageview_main);
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mImageView.setVisibility(View.INVISIBLE);
                        mWebView.setVisibility(View.VISIBLE);
                    }
                });
            }
        }, 2000);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            Log.i("*****++++++++++", mWebView.getUrl());
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTimer.cancel();
    }
}
